package pers.xiaojun.boot.web.core.filter;

import cn.hutool.core.util.StrUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.filter.OncePerRequestFilter;
import pers.xiaojun.boot.web.config.properties.WebProperties;


/**
 * API 请求过滤器
 *
 * @author xiaojun
 * @since 2025-11-21
 */
@RequiredArgsConstructor
public abstract class ApiRequestFilter extends OncePerRequestFilter {

    protected final WebProperties webProperties;

    /**
     * 判断请求路径是否需要过滤
     *
     * @param request 请求信息
     * @return 是否需要过滤
     */
    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) {
        // 只过滤 API 请求，如：/api/** /admin-api/** /app-api/**
        String apiUri = request.getRequestURI().substring(request.getContextPath().length());
        return !StrUtil.startWithAny(apiUri, webProperties.getApiPrefix(), webProperties.getAdminApiPrefix(), webProperties.getAppApiPrefix());
    }
}
